package com.example.churp;

import java.io.File;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Environment;
import android.widget.Button;
import android.view.View;
import android.content.DialogInterface;
import android.media.MediaRecorder;

public class Record extends Activity {
	Button start, stop, format;
	private MediaRecorder recorder = null;
	private int opformats[]= {MediaRecorder.OutputFormat.THREE_GPP};
	private String fileexten[] = {".3gpp" };
	private int curformat = 0;
	String finalfilename;

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record);

		start =(Button)findViewById(R.id.startbtn);
		stop =(Button)findViewById(R.id.stopbtn);
		format =(Button)findViewById(R.id.formatbtn);

		start.setOnClickListener(new btnClick());
		stop.setOnClickListener(new btnClick());
	}

	class btnClick implements View.OnClickListener {
		public void onClick(View v){
			switch(v.getId()){
			case R.id.startbtn:
				startRecording();
				break;
			case R.id.stopbtn:
				stopRecording();
				break;
			case R.id.formatbtn:
				break;
			}
		}
	}
	public void startRecording(){
		recorder = new MediaRecorder();

		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(opformats[curformat]);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		recorder.setOutputFile(getFilePath());

		try {
			recorder.prepare();
			recorder.start();
		} catch (Exception e){
			e.printStackTrace();
		}
	}

	public void stopRecording(){
			recorder.stop();
			recorder.reset();
			recorder.release();
	}

	private String getFilePath() {
		String filePath = Environment.getExternalStorageDirectory().getPath();
		File file = new File(filePath, "MediaRecorderSample");

		if(!file.exists()) {
			file.mkdir();
		}
		
		finalfilename=file.getAbsolutePath()+"/" + System.currentTimeMillis() + fileexten[curformat];
		return(finalfilename);
	}
}
